--[[
    文件用途：在加载配置时将拿到的配置转换为方便业务开发的配置
]]

local string = string
local table = table
local pairs = pairs
local table_insert = table.insert
local FOO = {}
local log               = require('modules.commhm.log')
local uu                = require("modules.commhm.utils")
--! @ args: config 福利服所有配置  cfg: welfare_gift 配置
FOO.deal_welf_gift = function(config, cfg)
    local get_up_key = function(item_id, gift_id)
        return string.format("%d_%d", item_id, gift_id)
    end
    -- 外挂的表格数据 概率up
    local rate_up = {}
    for _, v in pairs(config.welfare_gift_rate_up or {}) do
        local k = get_up_key(v.item_id, v.gift_id)
        rate_up[k] = v
    end

    -- 外挂的表格数据 定时产出
    local open_list = {}
    for _, v in pairs(config.welfare_gift_timer_open or {}) do
        open_list[v.item_id] = v
    end

    -- 外挂的表格数据 定时关闭
    local close_list = {}
    for _, v in pairs(config.welfare_gift_timer_close or {}) do
        open_list[v.item_id] = v
    end

    -- 整合cfg
    local ret = {
        xls={},
        itemid2giftid={},
    }

    for _, info in pairs(cfg or {}) do
        --整合必得物品
        info.must_item = {}
        info.check_item_type = {}
        local i = 1
        while true do
            local data = info["must"..i]
            if data then
                table.insert(info.must_item, {
                    id = data.id,
                    min = data.min,
                    max = data.max,
                    pool_rate = info[string.format("mustpoll%d_rate", i)]
                })
            else
                break
            end
            i = i + 1
        end
        --整合幸运物品
        for _, data in pairs(info.lucky1 or {}) do
            data.had_item = info.luckyeffect1[1]
            data.not_item = info.luckyeffect1[2]
            local k = get_up_key(data.id, info.id)
            data.rate_up = rate_up[k]           --挂载物品上升表
            data.open = open_list[data.id]      --挂载物品开启表
            data.close = close_list[data.id]    --挂载物品关闭表
            info.check_item_type[data.id] = 1
        end
        --整合随机物品
        info.random_item = {}
        i = 1
        while true do
            local data = info["random" .. i]

            if data then
                for _, v in pairs(data) do
                    local k = get_up_key(v.id, info.id)
                    v.rate_up = rate_up[k]           --挂载物品上升表
                    v.open = open_list[v.id]      --挂载物品开启表
                    v.close = close_list[v.id]    --挂载物品关闭表
                    info.check_item_type[v.id] = 1
                end
                local randomeffect = info['randomeffect'..i]
                local randomrate = info['randomrate'..i]
                table.insert(info.random_item, {
                    pool = data,
                    had_item = randomeffect[2],
                    not_item = randomeffect[1],
                    randomrate = randomrate,
                })
                
            else
                break
            end
            i = i + 1
        end
        ret.xls[info.id] = info     --以giftid作为index
        if info.item_id and info.item_id > 0 then
            ret.itemid2giftid[info.item_id] = info.id
        end
    end
    return ret
end


--! @ 礼包售卖表
FOO.deal_welfare_gift_mall = function(config, cfg)
    local ret = {}
    for k, v in pairs(cfg or {}) do
        ret[v.giftitem_id] = v
    end
    return ret
end


--! @ 礼包售卖表
FOO.deal_welfare_mount_mall = function(config, cfg)
    local ret = {}
    for k, v in pairs(cfg or {}) do
        ret[v.giftitem_id] = v
    end
    return ret
end


FOO.deal_welfare_lottery_mall = function(config, cfg)
    local ret = {}
    for k, v in pairs(cfg or {}) do
        ret[v.buy_id] = v
    end
    log.day_list("test", log.format_args("===: %%", ret))
    return ret
end


FOO.deal_welfare_lottery_exchange = function(config, cfg)
    local ret = {}
    for k, v in pairs(cfg or {}) do
        local items = {}
        for i=1, 10 do
            local id = v["itemid"..i]
            local num = v["itemnum"..i]
            if not (id and num) then
                break
            end 
            table.insert(items, {id=id,num=num})
        end
        v.items = items
        ret[v.index] = v
    end
    log.day_list("test1", log.format_args("===: %%", ret))
    return ret
end


FOO.deal_welfare_lottery_rewards = function(config, cfg)
    local ret = {}
    for k, v in pairs(cfg or {}) do
        local items = {}
        for i=1, 10 do
            local id = v["itemid"..i]
            local num = v["itemnum"..i]
            if not (id and num) then
                break
            end 
            table.insert(items, {id=id,num=num})
        end
        v.items = items
        ret[v.index] = v
    end
    log.day_list("test2", log.format_args("===: %%", ret))
    return ret
end


--! @ 幸运方块
FOO.deal_lucky_block = function (config, cfg)
    cfg = cfg or {}

    local buffers = {}
    local bufferitems = {}
    for _, v in pairs(cfg.item_buf or {}) do
        buffers[v.bufid] = v
        bufferitems[v.itemid] = v
    end
    cfg.buffers = buffers
    cfg.bufferitems = bufferitems

    local activity_rewards = {}
    for _, reward in pairs(cfg.activity_rewards or {}) do
        activity_rewards[reward.group] = activity_rewards[reward.group] or {}
        local group_reward = activity_rewards[reward.group]
        group_reward[#group_reward + 1] = reward
    end
    cfg.activity_rewards = activity_rewards

    local activitys = {}
    for _, act in pairs(cfg.activity or {}) do
        act.rewards = activity_rewards[act.reward]
        activitys[act.id] = act
    end
    cfg.activity = activitys

    return cfg
end

-- !@ 银享卡
FOO.deal_yin_xiang_card = function(config, cfg)
    cfg = cfg or {}

    local tres_config = {}
    for _, rcd in next, cfg do
        local t_newrcd = {}
        t_newrcd.name = rcd.Name
        t_newrcd.num  = rcd.Num
        t_newrcd.id   = rcd.ID
        t_newrcd.times = rcd.Time
        t_newrcd.prize = {}
        for i = 1, 4 do
            local key_id = "ItemID"..i
            local key_num = "Num"..i
            if rcd[key_id] ~= nil and rcd[key_num] ~= nil then
                table_insert(t_newrcd.prize, {itemid = rcd[key_id], itemnum=rcd[key_num]})
            end
        end
        tres_config[rcd.ID] = t_newrcd
    end

    return tres_config
end

--! @ 皮肤售卖
FOO.deal_welfare_skin_sale = function (config, cfg)
    local t_cfg       = {}
    local t_activitys = {}

    t_cfg.b_switch     = cfg.switch or false
    t_cfg.n_startTime  = (cfg.start_time or 0) / 1000
    t_cfg.n_endTime    = (cfg.end_time or 0) / 1000
    t_cfg.t_apiids     = cfg.apiids and uu.split(cfg.apiids, ',') or {}
    t_cfg.n_versionMin = uu.ver2i(cfg.version_min or '0.0.0')

    for k, v in pairs(cfg.skin_labels) do
        if v.activity_id then
            local t_tmp    = {}
            t_tmp.n_startTime  = (v.start_time or 0) / 1000
            t_tmp.n_endTime    = (v.end_time or 0) / 1000
            t_tmp.t_apiids     = v.apiids and uu.split(v.apiids, ',') or {}
            t_tmp.n_versionMin = uu.ver2i(v.version_min or '0.0.0')
            t_tmp.s_activityId = v.activity_id
            t_tmp.n_skinId     = v.skin_id or 0
            t_tmp.t_bonus      = {}

            for k2, v2 in pairs(v.bonus or {}) do
                if k2 and v2 and v2.reward_id and v2.reward_num then
                    t_tmp.t_bonus[k2]       = t_tmp.t_bonus[k2] or {}
                    t_tmp.t_bonus[k2].id    = v2.reward_id
                    t_tmp.t_bonus[k2].num   = v2.reward_num
                end
            end

            t_activitys[v.activity_id] = t_tmp
        end
    end

    t_cfg.t_skinLabels = t_activitys

    return t_cfg
end

return FOO